home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Freeware / Griffith 0.9.8 / griffith-0.9.8-win32.exe / {app} / lib / config.py < prev    next >
Text File  |  2008-11-17  |  7KB  |  203 lines

  1. # -*- coding: UTF-8 -*-
  2.  
  3. __revision__ = '$Id: config.py 908 2008-01-27 23:03:39Z piotrek $'
  4.  
  5. # Copyright (c) 2005-2008 Vasco Nunes, Piotr O┼╝arowski
  6. #
  7. # This program is free software; you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation; either version 2 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. # GNU Library General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with this program; if not, write to the Free Software
  19. # 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
  20.  
  21. # You may use and distribute this software under the terms of the
  22. # GNU General Public License, version 2 or later
  23.  
  24. import os
  25. import os.path
  26. import ConfigParser
  27. #import cPickle as pickle
  28.  
  29. class Config:
  30.     subst = {'True': True, 'False': False, 'None': None}
  31.     def __init__ (self, file):
  32.         self._file = file
  33.         if not self.load():
  34.             self.make_defaults()
  35.  
  36.         #self.set_hooks = []
  37.  
  38.     def get(self, option, default=None, section='main'):
  39.         if not isinstance(option, str):
  40.             option = str(option)
  41.         if not self._cfg.has_option(section, option):
  42.             return default
  43.         else:
  44.             value = self._cfg.get(section, option, False)
  45.             if value in self.subst.keys():
  46.                 value = self.subst[value]
  47.             return value
  48.  
  49.     def set(self, option, value, section='main'):
  50.         if not isinstance(option, str):
  51.             option = str(option)
  52.         if not isinstance(value, str):
  53.             value = str(value)
  54.         if not self._cfg.has_section(section):
  55.             self._cfg.add_section(section)
  56.         self._cfg.set(section, option, value)
  57.  
  58.     def has_key(self, key, section='main'):
  59.         return self._cfg.has_option(section,key)
  60.  
  61.     def __setitem__(self, k, v):
  62.         self.set(k,v, section='main')
  63.  
  64.     def __getitem__(self, key):
  65.         return self.get('main', key)
  66.  
  67.     def keys(self, section='main'):
  68.         return [ i[0] for i in self._cfg.items(section) ]
  69.     def values(self, section='main'):
  70.         return [ i[1] for i in self._cfg.items(section) ]
  71.     def items(self, section='main'):
  72.         return self._cfg.items(section)
  73.     def toDict(self, section='main'):
  74.         d = {}
  75.         for i,j in self._cfg.items(section):
  76.             d[i] = j
  77.         return d
  78.  
  79.     def save(self):
  80.         if not os.path.exists(os.path.split(self._file)[0]):
  81.             os.makedirs(os.path.split(self._file)[0])
  82.         self._cfg.write(open(self._file, 'w'))
  83.  
  84.     def load(self):
  85.         if os.path.isfile(self._file):
  86.             self._cfg = ConfigParser.SafeConfigParser()
  87.             try:
  88.                 self._cfg.read(self._file)
  89.             except:
  90.                 print 'Cannot parse config file'
  91.                 return False
  92.             # check values for number datatypes
  93.             # if current value could not be converted to number datatype
  94.             # the default is set instead
  95.             # some older config files need that correction but also in
  96.             # newer files is it possible, that the user inserts non-convertable
  97.             # values for number fields. The special problem is the string None.
  98.             tmp_value = self.get('color', 0, 'defaults')
  99.             try:
  100.                 int(tmp_value)
  101.             except:
  102.                 self.set('color', 0, 'defaults')
  103.             tmp_value = self.get('condition', 0, 'defaults')
  104.             try:
  105.                 int(tmp_value)
  106.             except:
  107.                 self.set('condition', 0, 'defaults')
  108.             tmp_value = self.get('layers', 0, 'defaults')
  109.             try:
  110.                 int(tmp_value)
  111.             except:
  112.                 self.set('layers', 0, 'defaults')
  113.             tmp_value = self.get('media', 0, 'defaults')
  114.             try:
  115.                 int(tmp_value)
  116.             except:
  117.                 self.set('media', 0, 'defaults')
  118.             tmp_value = self.get('region', 0, 'defaults')
  119.             try:
  120.                 int(tmp_value)
  121.             except:
  122.                 self.set('region', 0, 'defaults')
  123.             tmp_value = self.get('vcodec', 0, 'defaults')
  124.             try:
  125.                 int(tmp_value)
  126.             except:
  127.                 self.set('vcodec', 0, 'defaults')
  128.             return True
  129.         else:
  130.             return False
  131.             
  132.     def make_defaults(self):
  133.         self._cfg = ConfigParser.SafeConfigParser()
  134.         self._cfg.read(self._file)
  135.         self._cfg.add_section('database')
  136.         self._cfg.add_section('defaults')
  137.         self._cfg.add_section('window')
  138.         self._cfg.add_section('mainlist')
  139.         self._cfg.add_section('mail')
  140.         self._cfg.add_section('spell')
  141.         self._cfg.add_section('add')
  142.         self._cfg.add_section('main')
  143.         self._cfg.set('add', 's_cast', 'True')
  144.         self._cfg.set('add', 's_classification', 'True')
  145.         self._cfg.set('add', 's_country', 'True')
  146.         self._cfg.set('add', 's_director', 'True')
  147.         self._cfg.set('add', 's_genre', 'True')
  148.         self._cfg.set('add', 's_image', 'True')
  149.         self._cfg.set('add', 's_notes', 'True')
  150.         self._cfg.set('add', 's_o_site', 'True')
  151.         self._cfg.set('add', 's_o_title', 'True')
  152.         self._cfg.set('add', 's_plot', 'True')
  153.         self._cfg.set('add', 's_rating', 'True')
  154.         self._cfg.set('add', 's_runtime', 'True')
  155.         self._cfg.set('add', 's_site', 'True')
  156.         self._cfg.set('add', 's_studio', 'True')
  157.         self._cfg.set('add', 's_title', 'True')
  158.         self._cfg.set('add', 's_trailer', 'True')
  159.         self._cfg.set('add', 's_year', 'True')
  160.         self._cfg.set('database', 'file', 'griffith.db')
  161.         self._cfg.set('database', 'host', 'localhost')
  162.         self._cfg.set('database', 'name', 'griffith')
  163.         self._cfg.set('database', 'passwd', 'gRiFiTh')
  164.         self._cfg.set('database', 'port', '5432')
  165.         self._cfg.set('database', 'type', 'sqlite')
  166.         self._cfg.set('database', 'user', 'griffith')
  167.         self._cfg.set('defaults', 'color', '0')
  168.         self._cfg.set('defaults', 'condition', '0')
  169.         self._cfg.set('defaults', 'layers', '0')
  170.         self._cfg.set('defaults', 'media', '0')
  171.         self._cfg.set('defaults', 'region', '0')
  172.         self._cfg.set('defaults', 'vcodec', '0')
  173.         self._cfg.set('mail', 'email', 'griffith')
  174.         self._cfg.set('mail', 'password', '')
  175.         self._cfg.set('mail', 'smtp_server', 'localhost')
  176.         self._cfg.set('mail', 'use_auth', 'False')
  177.         self._cfg.set('mail', 'username', '')
  178.         self._cfg.set('mail', 'mail_smtp_port', '25')
  179.         self._cfg.set('mail', 'mail_use_tls', 'False')
  180.         self._cfg.set('main', 'default_movie_plugin', 'IMDB')
  181.         self._cfg.set('main', 'font', '')
  182.         self._cfg.set('main', 'pdf_reader', 'xpdf')
  183.         self._cfg.set('main', 'posters', 'posters')
  184.         self._cfg.set('main', 'rating_image', '0')    # 0 = meter; 1 = stars
  185.         self._cfg.set('mainlist', 'director', 'True')
  186.         self._cfg.set('mainlist', 'image', 'True')
  187.         self._cfg.set('mainlist', 'limit', '0')        # limit search results to x items (0 -> no limits)
  188.         self._cfg.set('mainlist', 'number', 'True')
  189.         self._cfg.set('mainlist', 'otitle', 'True')
  190.         self._cfg.set('mainlist', 'sortby', 'number')
  191.         self._cfg.set('mainlist', 'sortby_reverse', 'False')
  192.         self._cfg.set('mainlist', 'title', 'True')
  193.         self._cfg.set('spell', 'gtkspell', 'True')
  194.         self._cfg.set('spell', 'lang', 'en')
  195.         self._cfg.set('spell', 'notes', 'True')
  196.         self._cfg.set('spell', 'plot', 'True')
  197.         self._cfg.set('window', 'height', '700')
  198.         self._cfg.set('window', 'left', 'None')
  199.         self._cfg.set('window', 'top', 'None')
  200.         self._cfg.set('window', 'view_toolbar', 'True')
  201.         self._cfg.set('window', 'width', '500')
  202.         self.save()
  203.